package com.caigou.component;

import com.caigou.bean.cms.param.ChannelSpecialIssueParam;
import com.caigou.mapper.cms.ChannelSpecialIssueMapper;
import com.caigou.pojo.ChannelSpecialIssue;
import com.caigou.pojo.ChannelSpecialIssueTitle;
import com.caigou.service.ChannelSpecialIssueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class HtmlBuildSpecialIssueComponent {

    @Resource
    private ChannelSpecialIssueMapper channelSpecialIssueMapper;

    @Resource
    private TemplateComponent templateComponent;

    public void buildSpecialIssueHtml(String plateId){
        Map<String, Object> map = new HashMap<>();
        String templateCode = "";
        if ("101010601".equals(plateId)) {// 首页频道特刊
            ChannelSpecialIssueParam param = new ChannelSpecialIssueParam();
            param.setPlateId("101010601");
            ChannelSpecialIssueTitle title = channelSpecialIssueMapper.loadChannelSpecialIssueTitle(param);
            map.put("title", title);
            List<ChannelSpecialIssue> list = channelSpecialIssueMapper.loadChannelSpecialIssueList(param);
            map.put("list", list);
            templateCode = "HOMEFRAGMENTINDEXSPECIALISSUE";
        }
        // 如果没有捕获到模板编号--则代表当前广告位不需要更新静态页，因此return出去
        if (templateCode.equals("")) {
            log.info("当前位置不需要生成静态页--{}",plateId);
            return;
        }
        // 进行静态页生成方法调用
        try {
            templateComponent.createHtml(templateCode, map);
        } catch (IOException e){
            log.error("静态文件生成失败，失败原因是={}", e.getMessage());
        }
    }
}
